package cn.powerone.scm.controller;

import java.io.IOException;
import java.util.*;

import cn.powerone.common.utils.poi.*;
import cn.powerone.scm.domain.ScmCustomerAddress;
import cn.powerone.scm.domain.ScmCustomerContacts;
import cn.powerone.scm.service.IScmCustomerAddressService;
import cn.powerone.scm.service.IScmCustomerContactsService;
import cn.powerone.scm.upload.domain.ScmImportInquiriesIn;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.domain.ScmCustomer;
import cn.powerone.scm.service.IScmCustomerService;
import cn.powerone.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户Controller
 * 
 * @author PowerOne
 * @date 2025-06-23
 */
@Slf4j
@RestController
@RequestMapping("/scm/customer")
@RequiredArgsConstructor
public class ScmCustomerController extends BaseController
{
    private final IScmCustomerService scmCustomerService;
    private final IScmCustomerContactsService scmCustomerContactsService;
    private final IScmCustomerAddressService scmCustomerAddressService;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmCustomer scmCustomer)
    {
        startPage();
        List<ScmCustomer> list = scmCustomerService.selectScmCustomerList(scmCustomer);
        return getDataTable(list);
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
         return success(scmCustomerService.selectScmCustomerById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmCustomer scmCustomer)
    {
        scmCustomer.setCreateBy(getUsername());
        return toAjax(scmCustomerService.insertScmCustomer(scmCustomer));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmCustomer scmCustomer)
    {
        return toAjax(scmCustomerService.updateScmCustomer(scmCustomer));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmCustomerService.deleteScmCustomerByIds(ids));
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmCustomer scmCustomer) throws IOException
    {
        List<ScmCustomer> list = scmCustomerService.selectScmCustomerList(scmCustomer);
//        ExcelUtil<ScmCustomer> util = new ExcelUtil<ScmCustomer>(ScmCustomer.class);
//        util.exportExcel(response, list, "客户数据");

        List<ScmCustomerContacts> contactsList = scmCustomerContactsService.selectScmCustomerContactsList(null);
        for (ScmCustomerContacts contacts : contactsList){
            contacts.setCustomerNo(scmCustomerService.selectScmCustomerById(contacts.getCustomerId()).getCustomerNo());
        }
        List<ScmCustomerAddress> addressList = scmCustomerAddressService.selectScmCustomerAddressList(null);
        for (ScmCustomerAddress address : addressList){
            address.setCustomerNo(scmCustomerService.selectScmCustomerById(address.getCustomerId()).getCustomerNo());
        }
        ExcelExp firstSheet = new ExcelExp("客户信息", list, ScmCustomer.class);
        ExcelExp secondSheet = new ExcelExp("联系人", contactsList, ScmCustomerContacts.class);
        ExcelExp thirdSheet = new ExcelExp("地址", addressList, ScmCustomerAddress.class);
        List<ExcelExp> mySheet = new ArrayList<>();
        mySheet.add(firstSheet);
        mySheet.add(secondSheet);
        mySheet.add(thirdSheet);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mySheet);
        util.exportExcelManySheet(response, mySheet);
    }

    /**
     * 导入模板下载
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:import')")
    @Log(title = "客户", businessType = BusinessType.IMPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
//        ExcelUtil<ScmCustomer> util = new ExcelUtil<ScmCustomer>(ScmCustomer.class);
//        util.importTemplateExcel(response, "客户信息");
        //new way
        ExcelExp firstSheet = new ExcelExp("客户信息", null, ScmCustomer.class);
        ExcelExp secondSheet = new ExcelExp("联系人", null, ScmCustomerContacts.class);
        ExcelExp thirdSheet = new ExcelExp("地址", null, ScmCustomerAddress.class);
        List<ExcelExp> mySheet = new ArrayList<>();
        mySheet.add(firstSheet);
        mySheet.add(secondSheet);
        mySheet.add(thirdSheet);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mySheet);
        util.exportExcelManySheet(response, mySheet);
    }
    /**
     * 导入数据
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:import')")
    @Log(title = "客户", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
//        ExcelUtil<ScmCustomer> util = new ExcelUtil<ScmCustomer>(ScmCustomer.class);
//        List<ScmCustomer> scmCustomer = util.importExcel(file.getInputStream());
//        String operName = getUsername();
//        String message = scmCustomerService.importData(scmCustomer, updateSupport, operName);
//        return success(message);

        // 导入多个sheet的数据
        Map<String, List<?>> importDataMap = ExcelUtilManySheet.importMultipleSheets(file,
                Arrays.asList("客户信息", "联系人","地址"),
                new Class[]{ScmCustomer.class, ScmCustomerContacts.class,ScmCustomerAddress.class});

        // 处理导入的数据
        List<ScmCustomer> scmCustomerList = (List<ScmCustomer>) importDataMap.get("客户信息");
        List<ScmCustomerContacts> contactsList = (List<ScmCustomerContacts>) importDataMap.get("联系人");
        List<ScmCustomerAddress> addressList = (List<ScmCustomerAddress>) importDataMap.get("地址");

        String operName = getUsername();
        String message = scmCustomerService.importData(scmCustomerList, updateSupport, operName);
        scmCustomerContactsService.importData(contactsList, updateSupport, operName);
        scmCustomerAddressService.importData(addressList, updateSupport, operName);

        return AjaxResult.success("导入成功");
    }

}
